﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Configuration.Install;
using System.Linq;
using Microsoft.Win32;


namespace VOEISHubPushServiceClient
{
    [RunInstaller(true)]
    public partial class Installer1 : System.Configuration.Install.Installer
    {
        public Installer1()
        {
            InitializeComponent();
        }

        [System.Security.Permissions.SecurityPermission(System.Security.Permissions.SecurityAction.Demand)]
        public override void Install(IDictionary stateSaver)
        {
            base.Install(stateSaver);
        }

        [System.Security.Permissions.SecurityPermission(System.Security.Permissions.SecurityAction.Demand)]
        public override void Commit(IDictionary savedState)
        {
            base.Commit(savedState);
            //Get the install path from the registry key that we created in the setup project
            string installPath = "Test2";

            using (RegistryKey key = Registry.LocalMachine.OpenSubKey("Software\\Microsoft"))
            {
                installPath = (string)key.GetValue("VOEISHubPushServiceAppPath");
            }

            //Open the configuration file and set the config path to the VOEISHubPushService
            Configuration config = ConfigurationManager.OpenExeConfiguration(installPath + "VOEISHubPushServiceClient.exe");

            ClientSettingsSection section = (ClientSettingsSection)config.GetSectionGroup("applicationSettings").Sections.Get("VOEISHubPushServiceClient.Properties.Settings");

            dynamic origElement = section.Settings.Get("VOEISHubPushServicePath");
            section.Settings.Remove(origElement);
            origElement.Value.ValueXml.InnerXml = installPath + "VOEISHubPushServiceClient.exe";
            section.Settings.Add(origElement);

            // Save the current configuration.
            config.Save(ConfigurationSaveMode.Full);
            
        }

        [System.Security.Permissions.SecurityPermission(System.Security.Permissions.SecurityAction.Demand)]
        public override void Rollback(IDictionary savedState)
        {
            base.Rollback(savedState);
        }

        [System.Security.Permissions.SecurityPermission(System.Security.Permissions.SecurityAction.Demand)]
        public override void Uninstall(IDictionary savedState)
        {
            base.Uninstall(savedState);
        }

    }
}
